集合了一些關於Console的資料以及自己練習範例在以下![]()
C#中的console表示控制台。 Console 是一個類別 主要用於控制台應用程式的輸入和輸岀操作。
class Program
    {
        static void Main(string[] args)
        {
            /*輸出訊息(把數據輸出到控制台並顯示出來)*/
            Console.Write(""); //表示向控制台直接寫入字串,不進行換行,可繼續接著前面的字寫入。
            
            Console.WriteLine(); //表示向控制台寫入字串後 換行。
            /*輸入訊息(取得用戶輸入的資料)*/
            Console.Read(); //表示從控制台讀取字串,不換行。
            
            Console.ReadLine(); //表示從控制台讀取字串後進行換行。
            
            Console.ReadKey(); //取得鍵盤按下的下一個字符或功能鍵,按下的鍵顯示在控制台畫面中。
           /*其他功能*/
            Console.Beep(); //通過控制台播放提示音。
            Console.Clear(); //清除控制台緩衝區和相應的控制台畫面的顯示訊息。
        }
    }
範例
//Console.Write(格式化字串, 輸出項, 輸出項2);
//索引從0開始
class Program
    {
        static void Main(string[] args)
        {
            //請實作 輸入姓名和學校,並在輸出時組成一句話“xx 同學在xx 上課”
            Console.WriteLine("請輸入學生姓名:");
            string name = Console.ReadLine();
            Console.WriteLine("請輸入所在學校:");
            string school = Console.ReadLine();
            Console.WriteLine($"{name}同學在{school}上課");
            //也可寫作Console.WriteLine("{0}同學在{1}上課", name, school);
            
            
        }
    }
顯示結果如下:
常見格式



一些輸出範例
Console.WriteLine("{0, 8 :C}", 2); // $2.00
            Console.WriteLine("{0, 8 :C3}", 2); // $2.000
            Console.WriteLine("{0 :D3}", 2); // 002
            Console.WriteLine("{0 :E}", 2); // 2.000000E+000
            Console.WriteLine("{0 :G}", 2); // 2
            Console.WriteLine("{0 :N}", 2500000.00); // 2,500,00.00
            Console.WriteLine("{0 :x4}", 12); // 000c
            Console.WriteLine("{0, 2 :x}", 12); // c
            Console.WriteLine("{0 :000.000}", 12.23); // 012.230
            Console.WriteLine("{0 :r}", 15.62);// 15.62
            Console.WriteLine("{0 :d}", System.DateTime.Now); // 2022-3-17
            Console.WriteLine("{0 :D}", System.DateTime.Now); // 2022年3月17日
 
            Console.WriteLine("{0 :t}", System.DateTime.Now); // 11:43
            Console.WriteLine("{0 :T}", System.DateTime.Now); // 11:43:34
 
            Console.WriteLine("{0 :f}", System.DateTime.Now); // 2022年3月27日11:43
            Console.WriteLine("{0 :F}", System.DateTime.Now); // 2022年3月27日11:43:34
 
            Console.WriteLine("{0 :g}", System.DateTime.Now); // 2022-3-17 11:43
            Console.WriteLine("{0 :G}", System.DateTime.Now); // 2022-3-17 11:43:34
 
            Console.WriteLine("{0 :M}", System.DateTime.Now); // 3月17日
            Console.WriteLine("{0 :r}", System.DateTime.Now);// Thu, 17 Mar 2022 11:43:34 GMT
            Console.WriteLine("{0 :s}", System.DateTime.Now); // 2022-03-17T11:43:34
            Console.WriteLine("{0 :u}", System.DateTime.Now); // 2022-03-17 11:43:34Z
            Console.WriteLine("{0 :U}", System.DateTime.Now); // 2022年3月17日3:43:34
            Console.WriteLine("{0 :Y}", System.DateTime.Now); // 2022年3月
 
            Console.WriteLine("{0 :dd}", System.DateTime.Now); // 27
            Console.WriteLine("{0 :ddd}", System.DateTime.Now); // 四
            Console.WriteLine("{0 :dddd}", System.DateTime.Now); // 星期四
 
            Console.WriteLine("{0 :f}", System.DateTime.Now); // 2022年3月17日11:46
            Console.WriteLine("{0 :ff}", System.DateTime.Now); // 18
            Console.WriteLine("{0 :fff}", System.DateTime.Now); // 187
            Console.WriteLine("{0 :ffff}", System.DateTime.Now); // 1875
            Console.WriteLine("{0 :fffff}", System.DateTime.Now); // 18750
 
            Console.WriteLine("{0 :gg}", System.DateTime.Now); // 公元
            Console.WriteLine("{0 :ggg}", System.DateTime.Now); // 公元
            Console.WriteLine("{0 :gggg}", System.DateTime.Now); // 公元
            Console.WriteLine("{0 :ggggg}", System.DateTime.Now); // 公元
            Console.WriteLine("{0 :gggggg}", System.DateTime.Now);// 公元
 
            Console.WriteLine("{0 :hh}", System.DateTime.Now); // 11
            Console.WriteLine("{0 :HH}", System.DateTime.Now); // 11
 
            Console.WriteLine("{0 :mm}", System.DateTime.Now); // 50
            Console.WriteLine("{0 :MM}", System.DateTime.Now); // 03
 
            Console.WriteLine("{0 :MMM}", System.DateTime.Now); // 三月
            Console.WriteLine("{0 :MMMM}", System.DateTime.Now); // 三月
 
            Console.WriteLine("{0 :ss}", System.DateTime.Now); // 43
            Console.WriteLine("{0 :tt}", System.DateTime.Now); // 上午
 
            Console.WriteLine("{0 :yy}", System.DateTime.Now); // 12
            Console.WriteLine("{0 :yyyy}", System.DateTime.Now); // 2022
            Console.WriteLine("{0 :zz}", System.DateTime.Now); // +08
            Console.WriteLine("{0 :zzz}", System.DateTime.Now); // +08:00
            Console.WriteLine("{0 :hh:mm:ss}", System.DateTime.Now); // 11:43:34
            Console.WriteLine("{0 :dd/MM/yyyy}", System.DateTime.Now); // 17-03-2022
注意:
Console.ReadLine()和Console.Read()的輸入結果完全不同,不能混用。
Console.Read(), 返回值為首字的ASCII碼
Console.ReadLine(), 返回值為字串。
也就是說read方法只能讀取第一個字符,而ReadLine能讀多個字符也可以換行讀取
Console.ReadKey()的作用:
using System;
using System.Collections.Generic;
using System.Linq;using System.Text;
using System.Threading.Tasks;namespace ConsoleTest
{    class Program
    {        static void Main(string[] args)
        {
            Console.WriteLine("輸入用戶姓名和ID");           
            string name = Console.ReadLine();           
            int id = int.Parse(Console.ReadLine());
            Console.WriteLine("User Name is {0} \nThe id is {1}",name, id);
            Console.ReadKey();
        }
    }
read是從控制台讀取,key表示按下鍵盤,那麼組合在一起的意思就是取得用戶按下功能鍵顯示在視窗中,用在前面的語法達到視窗暫停的功能,在運行程式狀態下,只有按下任意鍵後視窗才會關閉。
參考來源:
C#中的console是什麼意思
百度知道 搜尋
C# Console.WriteLine()函數中{}輸出格式詳解